//
//  HHTweetController.m
//  oschina
//
//  Created by hjl on 16/4/1.
//  Copyright © 2016年 hjl. All rights reserved.
//

#import "HHTweetController.h"
#import "HHAccount.h"
#import "HHAccountTool.h"
#import <MJExtension.h>
#import "HHTweet.h"
#import "HHTweetCell.h"
#import <MJRefresh.h>

@interface HHTweetController ()

/** 模型数组 */
@property (nonatomic, strong) NSMutableArray *tweets;

/** 页码 */
@property (nonatomic, assign) int page;

@end

@implementation HHTweetController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setupTableView];
    
    [self loadNewTweet];
    
    [self refreshTableView];
    
}

- (void)refreshTableView{
    // 创建下拉刷新控件
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 网络刷新
        [self loadNewTweet];
        // 回调时结束刷新
        [self.tableView.mj_header endRefreshing];
    }];
    // 开始下拉刷新
    [self.tableView.mj_header beginRefreshing];
    

    // 创建上拉加载更多控件
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        // 回调时结束记载更多
        [self.tableView.mj_footer endRefreshing];
        // 加载更多数据
        [self loadMoreTweet];
    }];
    // 开始上拉加载更多
    [self.tableView.mj_footer beginRefreshing];
}

- (void)setupTableView{
    self.tableView.backgroundColor = HHRGBColor(245, 245, 245, 1.0);
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HHTweetCell class]) bundle:nil] forCellReuseIdentifier:@"HHTweetCell"];
    self.tableView.contentInset = UIEdgeInsetsMake(44, 0, 108, 0);
    self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
}



- (void)loadNewTweet{
    self.page = 1;
    
    HHAccount *account = [HHAccountTool account];
    
    NSString *str = @"/action/openapi/tweet_list";
    NSString *strUrl = [NSString stringWithFormat:@"%@%@", HHStrUrl, str];

    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"user"] = @(self.type);
    parameters[@"pageSize"] = @(20);
    parameters[@"page"] = @(self.page);
    parameters[@"dataType"] = @"json";
    parameters[@"access_token"] = account.access_token;
    
    
    [[AFHTTPSessionManager manager] POST:strUrl parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.tweets = [HHTweet mj_objectArrayWithKeyValuesArray:responseObject[@"tweetlist"]];

        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        // 加载错误时结束刷新
        [self.tableView.mj_header endRefreshing];
    }];
    
}

- (void)loadMoreTweet{
    self.page++;
    HHAccount *account = [HHAccountTool account];
    
    NSString *str = @"/action/openapi/tweet_list";
    NSString *strUrl = [NSString stringWithFormat:@"%@%@", HHStrUrl, str];
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"user"] = @(self.type);
    parameters[@"pageSize"] = @(20);
    parameters[@"page"] = @(self.page);
    parameters[@"dataType"] = @"json";
    parameters[@"access_token"] = account.access_token;
    
    
    [[AFHTTPSessionManager manager] POST:strUrl parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *moreTweets = [HHTweet mj_objectArrayWithKeyValuesArray:responseObject[@"tweetlist"]];
        
        [self.tweets addObjectsFromArray:moreTweets];
        
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 加载错误时结束刷新
        [self.tableView.mj_footer endRefreshing];
        _page--;
    }];
}



#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tweets.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HHTweetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HHTweetCell" forIndexPath:indexPath];
    
    HHTweet *tweet = self.tweets[indexPath.row];
    
    cell.tweet = tweet;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    HHTweet *tweet = self.tweets[indexPath.row];
    return tweet.cellHeight;
}

// 选中某行时,调用此方法.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //HHTweet *tweet = self.tweets[indexPath.row];
    
    HHAccount *account = [HHAccountTool account];
    NSString *str = @"/action/openapi/tweet_detail";
    NSString *strUrl = [NSString stringWithFormat:@"%@%@", HHStrUrl, str];
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"id"] = @(1);
    parameters[@"dataType"] = @"json";
    parameters[@"access_token"] = account.access_token;
    
    
    [[AFHTTPSessionManager manager] POST:strUrl parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        HHLog(@"%@", responseObject);
        
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        // 加载错误时结束刷新
        [self.tableView.mj_header endRefreshing];
    }];

}


@end
